package cn.iocoder.yudao.module.aiposter.service.submit;

import cn.iocoder.yudao.module.aiposter.dal.mysql.submit.AppSubmitMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.aiposter.controller.app.submit.vo.*;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.submit.SubmitDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.aiposter.enums.ErrorCodeConstants.*;

/**
 * 提交海报信息 Service 实现类
 *
 * @author Rain
 */
@Service
@Validated
public class AppSubmitServiceImpl implements AppSubmitService {

    @Resource
    private AppSubmitMapper appSubmitMapper;

    @Override
    public Long createSubmit(AppSubmitSaveReqVO createReqVO) {
        // 插入
        SubmitDO submit = BeanUtils.toBean(createReqVO, SubmitDO.class);
        appSubmitMapper.insert(submit);
        // 返回
        return submit.getId();
    }

    @Override
    public void updateSubmit(AppSubmitSaveReqVO updateReqVO) {
        // 校验存在
        validateSubmitExists(updateReqVO.getId());
        // 更新
        SubmitDO updateObj = BeanUtils.toBean(updateReqVO, SubmitDO.class);
        appSubmitMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubmit(Long id) {
        // 校验存在
        validateSubmitExists(id);
        // 删除
        appSubmitMapper.deleteById(id);
    }

    private void validateSubmitExists(Long id) {
        if (appSubmitMapper.selectById(id) == null) {
            throw exception(SUBMIT_NOT_EXISTS);
        }
    }

    @Override
    public SubmitDO getSubmit(Long id) {
        return appSubmitMapper.selectById(id);
    }

    @Override
    public PageResult<SubmitDO> getSubmitPage(AppSubmitPageReqVO pageReqVO) {
        return appSubmitMapper.selectPage(pageReqVO);
    }

}